package cl.migue.itunes.api;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import java.util.List;

/**
 *
 * @author Miguel A. Fuentes Buchholtz
 * @email contacto AT migue DOT cl
 */
public class ITunesSearchAPI {
    static final Client client = Client.create();
    static final WebResource service = client.resource("http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/wa/wsSearch");

    public synchronized static String searchItem(List<String> keywords, int keywordsnumber, int top) {
        return searchItem(keywords.toArray(new String[keywords.size()]),keywordsnumber,top);
    }

    public synchronized static String searchItem(java.lang.String[] keywords, int keywordsnumber, int top) {
        if(keywords.length > 0){
        String term = keywords[0];
        for (int i = 1; i < keywordsnumber; i++) {
            if(i<keywords.length){
                term = term + "+"+keywords[i];
            }
        }
        return service.queryParam("term", term).queryParam("limit", top+"").queryParam("kind", "music").get(String.class);
        }else{
            return "";
        }
    }
    
}
